package com.gouda.core.service.impl.advertisement;

import com.component.upload.api.IFileUploader;
import com.component.upload.enums.ImageRatio;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.gouda.core.dao.AdvertisementCityDao;
import com.gouda.core.dao.AdvertisementDao;
import com.gouda.core.dao.AdvertisementTypeDao;
import com.gouda.core.dao.UseCityDao;
import com.gouda.core.dto.advertisement.AdvertisementDto;
import com.gouda.core.entity.AdvertisementCityEntity;
import com.gouda.core.entity.AdvertisementEntity;
import com.gouda.core.entity.AdvertisementTypeEntity;
import com.gouda.core.entity.UseCityEntity;
import com.gouda.core.enums.GeneralStateEnum;
import com.gouda.core.enums.PlatformEnum;
import com.gouda.core.service.advertisement.AdvertisementService;
import com.hephaestus.utils.DataUtil;
import org.amethyst.atlas.basic.manufactory.specifics.DBEntityFactory;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by souu on 15/12/5.
 */
@Service
public class AdvertisementServiceImpl implements AdvertisementService {

    @Autowired
    private UseCityDao useCityDao;

    @Autowired
    private AdvertisementDao advertisementDao;

    @Autowired
    private AdvertisementCityDao advertisementCityDao;

    @Autowired
    private AdvertisementTypeDao advertisementTypeDao;

    @Autowired
    private IFileUploader fileUploader;


    @Override
    public int createAdvertisement(String title, String introduction, GeneralStateEnum defaultEnum,
                                   Date startTime, Date endTime, Set<Long> cityIds, List<Long> picIds,
                                   List<Integer> platforms, List<String> skipUrls) {
        if (Strings.isNullOrEmpty(title) || CollectionUtils.isEmpty(picIds)
                || CollectionUtils.isEmpty(platforms) || CollectionUtils.isEmpty(skipUrls)) {
            return -1;
        }

        if (defaultEnum == GeneralStateEnum.NO && (startTime == null || endTime == null
                || CollectionUtils.isEmpty(cityIds))) {
            return -1;
        }

        for (int platform : platforms) {
            if (PlatformEnum.fromInteger(platform) == null) {
                return -1;
            }
        }

        if (!CollectionUtils.isEmpty(cityIds)) {
            for (long cityId : cityIds) {
                if (useCityDao.unique(UseCityEntity.class, cityId) == null) {
                    return -1;
                }
            }
        }

        AdvertisementEntity advertisement = DBEntityFactory.getFactory().generateDBEntity(AdvertisementEntity.class);
        advertisement.setCreateTime(new Date());
        advertisement.setEndTime(endTime);
        advertisement.setIntroduction(introduction);
        advertisement.setIsdefault(defaultEnum.getCode());
        advertisement.setStartTime(startTime);
        advertisement.setState(GeneralStateEnum.NO.getCode());
        advertisement.setTitle(title);
        this.advertisementDao.insert(advertisement);

        for (long cityId : cityIds) {
            AdvertisementCityEntity advertisementCity = DBEntityFactory.getFactory().generateDBEntity(AdvertisementCityEntity.class);
            advertisementCity.setAdId(advertisement.getId());
            advertisementCity.setCityId(cityId);
            advertisementCity.setCreateTime(new Date());
            advertisementCity.setState(GeneralStateEnum.NO.getCode());
            this.advertisementCityDao.insert(advertisementCity);
        }

        for (int i = 0; i < platforms.size(); i++) {
            AdvertisementTypeEntity advertisementType = DBEntityFactory.getFactory().generateDBEntity(AdvertisementTypeEntity.class);
            advertisementType.setCreateTime(new Date());
            advertisementType.setImageId(picIds.get(i) + "");
            advertisementType.setPlatform(platforms.get(i));
            advertisementType.setSkipUrl(skipUrls.get(i));
            this.advertisementTypeDao.insert(advertisementType);
        }

        return 1;

    }

    @Override
    public void delAdvertisement(long id) {
        if (id <= 0) {
            return;
        }
        AdvertisementEntity advertisement = this.advertisementDao.unique(AdvertisementEntity.class, id);
        if (advertisement == null) {
            return;
        }
        advertisement.setState(GeneralStateEnum.DELETE.getCode());
        this.advertisementDao.update(advertisement);

        List<AdvertisementCityEntity> advertisementCites = this.advertisementCityDao.getAdvertisementCityByAdId(id);
        for (AdvertisementCityEntity advertisementCity : advertisementCites) {
            advertisementCity.setState(GeneralStateEnum.DELETE.getCode());
            this.advertisementCityDao.update(advertisementCity);
        }


    }

    @Override
    public void updateAdvertisementOnline(long id) {
        if (id <= 0) {
            return;
        }

        AdvertisementEntity advertisement = this.advertisementDao.unique(AdvertisementEntity.class, id);
        if (advertisement == null) {
            return;
        }

        if (GeneralStateEnum.fromInteger(advertisement.getState()) == GeneralStateEnum.DELETE) {
            return;
        }
        advertisement.setState(GeneralStateEnum.YES.getCode());
        this.advertisementDao.update(advertisement);

        List<AdvertisementCityEntity> advertisementCites = this.advertisementCityDao.getAdvertisementCityByAdId(id);
        for (AdvertisementCityEntity advertisementCity : advertisementCites) {
            advertisementCity.setState(GeneralStateEnum.YES.getCode());
            this.advertisementCityDao.update(advertisementCity);
        }
    }

    @Override
    public void updateAdvertisementOffline(long id) {
        if (id <= 0) {
            return;
        }

        AdvertisementEntity advertisement = this.advertisementDao.unique(AdvertisementEntity.class, id);
        if (advertisement == null) {
            return;
        }
        if (GeneralStateEnum.fromInteger(advertisement.getState()) == GeneralStateEnum.DELETE) {
            return;
        }
        advertisement.setState(GeneralStateEnum.NO.getCode());
        this.advertisementDao.update(advertisement);
        List<AdvertisementCityEntity> advertisementCites = this.advertisementCityDao.getAdvertisementCityByAdId(id);
        for (AdvertisementCityEntity advertisementCity : advertisementCites) {
            advertisementCity.setState(GeneralStateEnum.NO.getCode());
            this.advertisementCityDao.update(advertisementCity);
        }
    }

    @Override
    public List<AdvertisementDto> getAdvertisementByCityIdAndPlatform(long cityId, PlatformEnum platform) {
        if (cityId <= 0 || platform == null) {
            return null;
        }

        List<Long> ids = this.advertisementCityDao.getAdvertisementIdByCityId(cityId);
        if (CollectionUtils.isEmpty(ids)) {
            ids = this.advertisementDao.getDefaultAdvertisement();
        }
        if (CollectionUtils.isEmpty(ids)) {
            return null;
        }
        List<AdvertisementDto> result = Lists.newArrayList();
        for (long id : ids) {
            AdvertisementEntity advertisement = this.advertisementDao.unique(AdvertisementEntity.class, id);
            if (GeneralStateEnum.fromInteger(advertisement.getState()) != GeneralStateEnum.YES) {
                continue;
            }
            if (advertisement.getStartTime() != null && advertisement.getStartTime().getTime() >= new Date().getTime()) {
                continue;
            }
            if (advertisement.getEndTime() != null && advertisement.getEndTime().getTime() <= new Date().getTime()) {
                continue;
            }

            AdvertisementTypeEntity advertisementType = this.advertisementTypeDao.getAdvertisementTypeByAdId(advertisement.getId(), platform);
            if (advertisementType == null) {
                continue;
            }
            result.add(this.copyDto(advertisement, advertisementType, cityId));
        }


        return result;
    }

    private AdvertisementDto copyDto(AdvertisementEntity advertisement, AdvertisementTypeEntity advertisementType, long cityId) {
        AdvertisementDto dto = new AdvertisementDto();
        dto.setCityId(cityId);
        dto.setEndTime(advertisement.getEndTime());
        dto.setId(advertisement.getId());
        dto.setImageId(advertisementType.getImageId());
        dto.setIntroduction(advertisement.getIntroduction());
        dto.setIsdefault(advertisement.getIsdefault());
        dto.setStartTime(advertisement.getStartTime());
        dto.setTitle(advertisement.getTitle());
        dto.setImageUrl(fileUploader.seekFile(DataUtil.parseL(advertisementType.getId()), ImageRatio.Original));
        dto.setPlatform(advertisementType.getPlatform());
        dto.setPlatformId(advertisementType.getId());
        dto.setSkipUrl(advertisementType.getSkipUrl());
        return dto;
    }
}
